package com.aiti.lulian.mapper;

import com.aiti.lulian.workflow.ProcessInstance;
import com.aiti.lulian.workflow.ProcessStatus;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;



import java.util.List;

public interface ProcessInstanceMapper extends BaseMapper<ProcessInstance> {

    default List<ProcessInstance> findByProcessDefinitionId(String processDefinitionId) {
        return selectList(new QueryWrapper<ProcessInstance>()
                .eq("process_definition_id", processDefinitionId));
    }

    default List<ProcessInstance> findByBusinessKey(String businessKey) {
        return selectList(new QueryWrapper<ProcessInstance>()
                .eq("business_key", businessKey));
    }

    default List<ProcessInstance> findByStatus(ProcessStatus status) {
        return selectList(new QueryWrapper<ProcessInstance>()
                .eq("status", status.name()));
    }

    default List<ProcessInstance> findByProcessDefinitionIdAndStatus(
            String processDefinitionId, ProcessStatus status) {
        return selectList(new QueryWrapper<ProcessInstance>()
                .eq("process_definition_id", processDefinitionId)
                .eq("status", status.name()));
    }

    @Select("SELECT * FROM dj_process_instance WHERE business_key LIKE CONCAT('%', #{businessKey}, '%')")
    List<ProcessInstance> findByBusinessKeyContaining(@Param("businessKey") String businessKey);
}